import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Myhome(),
    );
  }
}

class Myhome extends StatelessWidget {
  const Myhome({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(
          child: Text(
            "商品列表",
            textDirection: TextDirection.ltr,
            style: TextStyle(color: Colors.deepPurpleAccent),
          ),
        ),
        backgroundColor: Colors.green,
      ),
      body: Myhomebody(),
    );
  }
}

class Myhomebody extends StatelessWidget {
  const Myhomebody({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return HomePage();
  }
}

//Widget是不加下划线的：暴露给别人使用
class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _Homeaction();
  }
}

//State是加_状态这个类只是给Widget使用
/**
 * 1.为什么Flutter在设计的时候StatefulWidget的build方法放在Statte中
 * build出来的Widge是需要依赖State中国呢的变量（状态/数据）
 * 2.在flutter的运行过程中：
 * Widge是不断的销毁和创建的
 * 当我们自己的状态发生改变的时候，并不希望重新创建一个新的State
 */
class _Homeaction extends State<HomePage> {
  int? a = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              RaisedButton(
                  child: Icon(Icons.add),
                  color: Colors.orange,
                  onPressed: () {
                    setState(() {
                      a = (a! + 1);
                    });
                    print("+");
                  }),
              SizedBox(
                width: 10,
              ),
              RaisedButton(
                  child: Icon(Icons.dangerous),
                  color: Colors.orange,
                  onPressed: () {
                    setState(() {
                      a = (a! - 1);
                    });
                    print("_");
                  }),
            ],
          ),
          Text("当前计数$a")
        ],
      ),
    );
  }
}

